package com.changgou.order.controller;


import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import com.changgou.utils.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cart")
@CrossOrigin
public class CartController {

    @Autowired
    private CartService cartService;


    //获取用户信息的
    @Autowired
    private TokenDecode tokenDecode;

    /**
     * 加入购物车
     *
     * @param num   购买的数量
     * @param skuId 购买的商品skuId
     * @return
     */

    @RequestMapping("/add/{skuId}/{num}")
    public Result add(@PathVariable("num") Integer num, @PathVariable("skuId") Long skuId) {

//获取用户名
        String username = tokenDecode.getUserInfo().get("username");

        cartService.add(num, skuId, username);

        return new Result(true, StatusCode.OK, "添加购物车成功！");

    }

    @RequestMapping("/list")
    public Result findAll() {
        //获取用户名
        String username = tokenDecode.getUserInfo().get("username");
        List<OrderItem> list = cartService.list(username);

        return new Result(true, StatusCode.OK, "查询购物车成功！", list);

    }


}
